package com.lf.lexy.controller.rec;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.lf.comm.MyConstants;
import com.lf.lexy.business.BlogBusiness;
import com.lf.lexy.controller.BaseController;
import com.lf.lexy.dao.BlogContentDao;
import com.lf.lexy.dao.EnumsDao;
import com.lf.lexy.dao.UserDao;
import com.lf.lexy.model.BlogContent;
import com.lf.lexy.model.User;
import com.lf.lexy.model.dto.ResponeObj;
import com.lf.lexy.webutils.CommMethod;

public class RecController extends BaseController {

	
	private BlogBusiness blogBusiness = new BlogBusiness();

	
	
	

	/* 
	 * 主页
	 * 
	 * 
	 */
	public void index(){
		User user = (User)getSessionAttr(MyConstants.SESSION_USER);
		String userNo = null;
		if(user!=null){
			userNo = user.getUserNo();
		}
		
		List<Record> blogTypeList = blogBusiness.getBlogTypeList("999999");
		setAttr("blogTypeList", blogTypeList);
		Page<BlogContent> blogContentList = blogBusiness.getBlogContentList(userNo , null , "1" ,MyConstants.PAGE_SIZE_REC);
		setAttr("bloglist", blogContentList);
		setAttr("enFlag", MyConstants.ENFLAG);
//		render("rec_index.jsp");
		render("rec_index_copy.jsp");
	}
	
	
	
	
	  
	    
	  
	/**  
	* @Title: toRegisterPage  
	* @Description: TODO(这里用一句话描述这个方法的作用)  
	* @param 参数：      
	* @return 返回类型： void      
	* @author LiuFei
	* @date 下午9:58:09
	* @throws  
	*/  
	public void toRegisterPage(){
		render("rec_register.jsp");
	}
	  
	/**  
	* @Title: toLoginPage  
	* @Description: TODO(这里用一句话描述这个方法的作用)  
	* @param 参数：      
	* @return 返回类型： void      
	* @author LiuFei
	* @date 下午9:58:24
	* @throws  
	*/  
	public void toLoginPage(){
		render("rec_login.jsp");
	}
	
	
	
	public void loginIn(){
		
		ResponeObj obj = new ResponeObj();
		String userNo = super.getPar("userNo");
		String userMail = super.getPar("userMail");
		String userPwd = super.getPar("userPwd");
		
		
		//User user = UserDao.me.getUser(userNo, userPwd);
		
		User user = UserDao.me.geetUser(userNo, userMail, null, userPwd);
		if(user==null){
			obj.setCode("000");
			obj.setErrMsg("不存在该用户!");
			setAttr("responeObj", obj);
			render("rec_login.jsp");
		}
		if(user!=null){
			String isDelete = user.getIsDelete();
			if(MyConstants.IS_DELETE_Y.equals(isDelete)){
				obj.setCode("000");
				obj.setErrMsg("该用户被禁止登录,请联系管理员!");
				setAttr("responeObj", obj);
				render("rec_login.jsp");
			}else{
				setSessionAttr(MyConstants.SESSION_USER, user);
				//List<Record> blogTypeList = blogBusiness.getBlogTypeList("999999");
				//setAttr("blogTypeList", blogTypeList);
				Page<BlogContent> blogContentList = blogBusiness.getBlogContentList(userNo , null , "1" ,MyConstants.PAGE_SIZE_REC);
				setAttr("bloglist", blogContentList);
				setAttr("enFlag", MyConstants.ENFLAG);
				render("rec_index_copy.jsp");
			}
		}
	}
	
	
	
	
	 /**  
	* @Title: registerIn  
	* @Description: REC - 注册新用户
	* @param 参数：      
	* @return 返回类型： void      
	* @author LiuFei
	* @date 下午9:12:00
	* @throws  
	*/  
	public void  registerIn(){
		String par = super.getPar("reqStr");
		ResponeObj obj = new ResponeObj();
		HashMap<String, String> parseObject = MyConstants.json.parseObject(par , new HashMap<String, String>().getClass());
		String userMail = parseObject.get("user_mail");
		String userName = parseObject.get("user_name");
		String userPwd = parseObject.get("user_pwd");
		if(StringUtils.isNotEmpty(userMail) && StringUtils.isNotEmpty(userName) && StringUtils.isNotEmpty(userPwd)){
			//boolean insertUser = UserDao.me.insertUser(userNo, userPwd);
			boolean flag = UserDao.me.insertUser(null, userMail, userName, null, userPwd);
			if(flag){
				obj.setCode(MyConstants.CODE_SUCCESS);
			}
		}else{
			obj.setCode(MyConstants.CODE_FAILURE);
			obj.setErrMsg("注册信息不能为空！");
		}
		super.success(obj);
	}
	
	
	
	
	/**
	 * 
	 * 用户登录
	 * 
	 */
	public void signIn(){
		
		ResponeObj obj = new ResponeObj();
		Map<String , Object> map = new HashMap<String , Object>();
		
		String par = super.getPar("reqStr");
		par = getPara("reqStr");
		HashMap<String, String> parseObject = MyConstants.json.parseObject(par , new HashMap<String, String>().getClass());
		String userName = parseObject.get("name");
		String userPwd = parseObject.get("pwd");
		
		User user = UserDao.me.getUser(userName, userPwd);
		if(user==null){
			obj.setCode("000");
			obj.setErrMsg("不存在该用户!");
			super.success(obj);
			return;
		}
		if(user!=null){
			String isDelete = user.getIsDelete();
			if(MyConstants.IS_DELETE_Y.equals(isDelete)){
				obj.setCode("000");
				obj.setErrMsg("该用户被禁止登录,请联系管理员!");
				super.success(obj);
				return;
			}else{
				setSessionAttr(MyConstants.SESSION_USER, user);
				obj.setCode("200");
				obj.setErrMsg("登录成功！");
				super.success(obj);
				return;
			}
		}
	}
	
	/**
	 * 
	 *   用户退出， 清空session
	 *  
	 */
	public void sginOut(){
		getSession().removeAttribute("user");
		Map<String, Object> map = CommMethod.getMap();
		map.put("code", "200");
		super.success(map);
	}
	
	
	
	public void getBlogContentList(){
		User user = (User)getSessionAttr(MyConstants.SESSION_USER);
		String userNo = null;
		if(user!=null){
			userNo = user.getUserNo();
		}
		Map<String, Object> map = CommMethod.getMap();
		String pageNumber = super.getPar("pageNum");
		String tagVal = super.getPar("tagVal");
		Page<BlogContent> blogContentList = blogBusiness.getBlogContentList(null , tagVal , pageNumber ,MyConstants.PAGE_SIZE_REC);
		map.put("bloglist", blogContentList);
		super.success(map);
	}
	
	public void _user_getBlogList(){
		
		User user = (User)getSessionAttr(MyConstants.SESSION_USER);
		
		Map<String, Object> map = CommMethod.getMap();
		String pageNumber = super.getPar("pageNum").trim();
		String tagVal = super.getPar("tagVal").trim();
		Page<BlogContent> blogContentList = blogBusiness.getBlogContentList(null , tagVal , pageNumber ,MyConstants.PAGE_SIZE_REC);
		
		
		if(user!=null){
			
			List<BlogContent> list = blogContentList.getList();
			
			for (BlogContent obj : list) {
				if(obj.getUpdateUserNo().equals(user.getUserNo())){
					
				}
			}
			
			
			
		}
		
		
		
		
		
	}
	
	
	
	
	
	
	
	 /**  
	* @Title: getBlogContent  
	* @Description:  REC - 查看博文详情
	* @param 参数：      
	* @return 返回类型： void      
	* @author LiuFei
	* @date 下午9:06:03
	* @throws  
	*/  
	    
	public void getBlogContent(){
		String serialId = getPara("serialId");
		BlogContent obj = BlogContentDao.me.geetBlogContentByPK(serialId);
		
		//BlogContent resultObj = new BlogContent();
		if(obj!=null){
			String geetEnumsVal = EnumsDao.me.geetEnumsVal("_local_ip");
			System.out.println("_local_ip:"+geetEnumsVal);
			String _local_ip_port = "http://"+geetEnumsVal;
			String blogContent = obj.getBlogContent();
			//替换 ip 和 port
			blogContent = blogContent.replace("_ip_port", _local_ip_port);
			blogContent = blogContent.replace("http://127.0.0.1:8080", _local_ip_port);
			
			System.out.println(blogContent);
			Document doc = Jsoup.parse(blogContent);
			Elements divlist = doc.getElementsByAttributeValue("class", "file-list");
			
			//拼装html 代码
			String lgHtml = "<div class='file-list'>";
			for ( int i = 0 ; i < divlist.size() ; i++) {
				Elements element = divlist.get(i).getElementsByAttributeValue("style", "text-align:center");
				System.out.println(element.size());
				for (int j = 0; j < element.size(); j++) {
					Element element2 = element.get(j);
					Element child = element2.child(0);
					lgHtml += "<p style='text-align:center'>";
					lgHtml += "<a href='"+child.attr("src")+"' title='图片' data-gallery=''>";
					lgHtml += child.toString().replace("\"", "'");
					lgHtml += "</a>";
					lgHtml += "</p>";
				}
			}
			lgHtml += "</div>";
			obj.setBlogContent(lgHtml);
		}
		setAttr("title", obj.getBlogTitle());
		setAttr("blog", obj);
		render("rec_blog_content.jsp");
	}
}
